package com.ibm.law.ui;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;

import com.ibm.law.domain.Resource;
import com.ibm.law.service.global.ResourceService;

public class MenuTreeViewer extends TreeViewer {
	private static class TreeContentProvider implements ITreeContentProvider {
		ResourceService resourceService = new ResourceService();

		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}

		public void dispose() {
		}

		public Object[] getElements(Object inputElement) {
			if (inputElement.equals("Root")) {
				return resourceService.findRootResource().toArray();
			}
			return getChildren(inputElement);
		}

		public Object[] getChildren(Object parentElement) {
			Resource resource = (Resource) parentElement;
			return resource.getChildren().toArray();
		}

		public Object getParent(Object element) {
			Resource resource = (Resource) element;
			return resource.getParent();
		}

		public boolean hasChildren(Object element) {
			return getChildren(element).length > 0;
		}
	}

	private static class ViewerLabelProvider extends LabelProvider {
		public Image getImage(Object element) {
			return super.getImage(element);
		}

		public String getText(Object element) {
			Resource resource = (Resource) element;
			return super.getText(resource.getName());
		}
	}

	public MenuTreeViewer(Composite parent) {
		super(parent);
		this.setContentProvider(new TreeContentProvider());
		this.setLabelProvider(new ViewerLabelProvider());
//		this.getTree().setHeaderVisible(true);
//		this.getTree().setLinesVisible(true);
	}

}
